"""
  # File       : checkPoint.py
  # Time       : 2021/12/13
  # Author     : XGT
  # version    : V1.0.0
  # Description: 重写unittest断言方法,解决断言失败不再继续执行的问题
  # Note       : raise用于标记错误,方法核心，如失去便会一直提示正确，影响报告
"""

import unittest


def checkAssert_raise(arg1, arg2):
    """
       用于抛出异常，调整报告
    """
    raise AssertionError(arg1, arg2)

class CheckPoint(unittest.TestCase):


    def __init__(self,  methodName='runTest'):
        super(CheckPoint, self).__init__(methodName)
        self._testMethodName = methodName
        self.flag = 0
        self.msg = []

    def checkAssertEqual(self, arg1, arg2, msg=None):
        """
           检测参数1：arg1，参数2：arg2是否相等
           相等则返回True
           不相等则返回False
        """
        try:
            self.assertEqual(arg1, arg2, msg)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            return False



    def checkAssertNotEqual(self, arg1, arg2, msg=None):
        """
           检测参数1：arg1，参数2：arg2是否不相等
           不相等则返回True
           相等则返回False
        """
        try:
            self.assertNotEqual(arg1, arg2, msg)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkAssertTrue(self, expr, msg=None):
        """
           验证expr是true，如果为false，则fail
        """
        try:
            self.assertTrue(expr, msg)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkAssertFalse(self, expr, msg=None):
        """
           验证expr是false，如果为true，则fail
        """
        try:
            self.assertFalse(expr, msg)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    # 比较断言：比较两个变量的值
    def checkAssertGreater(self, first, second, msg=None):
        """　
           验证first > second，否则fail
        """
        try:
            self.assertGreater(first, second)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkAssertGreaterEqual(self, first, second, msg=None):
        """
           验证first >= second，否则fail
        """
        try:
            self.assertGreaterEqual(first, second)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkAssertLess(self, first, second, msg=None):
        """
           验证first < second，否则fail
        """
        try:
            self.assertLess(first, second)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkAssertLessEqual(self, first, second, msg=None):
        """
           验证first <= second，否则fail
        """
        try:
            self.assertLessEqual(first, second)
            return True
        except Exception as e:
            self.flag += 1
            self.msg.append("\n{}".format(msg))
            print(e)
            raise
            return False

    def checkTestResult(self):
        """
           获取用例执行结果，断言flag是否为0，不为0说明测试用例中存在断言失败
        """
        return self.assertEqual(self.flag, 0, "{}".format(self.msg))